var rootPath = getRootPath();	


$(document).ready(function(){
	
	var packageCode = getQueryParamValue("packageCode");
	if(packageCode!=undefined && packageCode!=''){
		$.ajax({
			method : 'get',
			url : rootPath+'/wechat/api/showroom/packages?packageCode='+packageCode,
			error:function(XMLHttpRequest, ajaxOptions, thrownError){
				alert("连接服务器错误!");
			},
			success : function(resultData){
		        $("#defaultBrandCode").val(resultData.brandCode);
		        $("#defaultSeriesCode").val(resultData.seriesCode);
		        $("#defaultModelCode").val(resultData.modelCode);
		        $("#defaultPackageCode").val(resultData.packageCode);
				
				$("#defaultPackageName").val(resultData.packageName);
			}
		});
	}
	 
	
	 var now = new Date();
	 var currYear = now.getFullYear();	
	 //选择日期
     $('#purchaseDate').mobiscroll().date({
         theme: 'ios',
         lang: 'zh',
         display: 'bottom',
         mode: 'datetimeDate',
         max: new Date(now.getFullYear(), now.getMonth(), now.getDate()),
         dateFormat:'yyyy-mm-dd'

     });
       
	 $('#selectLoveModelTopSure').click(function(){
		 $('#selectLoveModelTop').modal('hide');
     });	
	 
	 //确定绑定
	 $("#sureBinding").click(function(){
		 
		 var defaultPackageCodeTemp=$("#defaultPackageCode").val();
		 if(defaultPackageCodeTemp==undefined || defaultPackageCodeTemp==''){
				$('#selectLoveModelTop').modal({
					keyboard: false,
					backdrop:false
				});
			return;
		 }

	   	 $('#vehicleBindingForm').bootstrapValidator('validate');
	     var isValid=$("#vehicleBindingForm").data("bootstrapValidator").isValid();

	     if(!isValid){
	     	return;
	     }
	     
	     
	    var data={
	    	packageCode:"",
	    	licenseNo:"", 
	    	engineNo:"",
	    	vin:"", 
	    	purchaseDate:"",
	    	personalName:"",
	    	phone:""
	    }; 
	    
	    data.packageCode = $.trim($("#defaultPackageCode").val()); 
	    data.licenseNo = $.trim($("#licenseNo").val()); 
	    data.engineNo = $.trim($("#engineNo").val()); 
	    data.vin = $.trim($("#vin").val()); 
	    data.purchaseDate = $.trim($("#purchaseDate").val()); 
	    data.personalName = $.trim($("#personalName").val()); 
	    data.phone = $.trim($("#phone").val()); 
	    
	    $('#personalNameModal').html(data.personalName);
	    
		$.ajax({
			type : 'POST',
			url : rootPath+'/wechat/api/vehicleBinding',
			contentType:"application/json",
			data: JSON.stringify(data),
			error:function(XMLHttpRequest, ajaxOptions, thrownError){
				
			},
			success : function(data){
				$('#testdrivingAmModal').modal({
					keyboard: false,
					backdrop:false
				});
				window.location = rootPath + "/views/mt.html";
			}
				
		});
		
	    
		 
	 });
		
	//模态框确认
	$('#modalSure').click(function(){
	    var packageCode = $("#defaultPackageCode").val();

		$('#testdrivingAmModal').modal('hide');
	    $("#personalName").val(''); 
	    $("#phone").val(''); 
	    $("#defaultPackageCode").val(''); 
	    $("#salesConsultantId").val(''); 
	    $("#startDate").val(''); 
	    $("#appointmentContent").val(''); 
	    $('#vehicleBindingForm').data('bootstrapValidator').resetForm(true);
//			window.location.href = rootPath +"/views/newCarShowroom/packageExhibition.html?packageCode="+packageCode;
	});	
	 
     $("#vehicleBindingForm").addClass("active in");
     $("#carModelExhibitionDiv").removeClass("active in");
	 $("#carModelExhibitionDetailDiv").removeClass("active in");

     //选择品牌车系
     $(".selectBrandSeries").click(function(){
    	     	 
         var seriesCode = $("#defaultSeriesCode").val();
         
         //判断是否已经已有已选择的车系，车型，配置,如果存在直接进入车型配置选择页面，否则 进入品牌车系选择界面 
     	 if(seriesCode ==undefined || seriesCode==''){
     		 initBrandSeries();
         }else{
        	 initModelPackage(seriesCode);
         }
         
     });
     
     
     //从新选择品牌车系
     $(".againSelectBrandSeries").click(function(){
 		 initBrandSeries();
     });
 });


//初始化加载品牌车系界面 
function initBrandSeries(){
	
	var brandCode = $("#defaultBrandCode").val();
	if(brandCode ==undefined || brandCode==''){
		brandCode='';
	}
	$("#carModelExhibitionDiv").empty();
	
	$.ajax({
		method : 'get',
		url : rootPath+'/wechat/api/showroom/brands?brandCode='+brandCode,
		error:function(XMLHttpRequest, ajaxOptions, thrownError){
			alert("连接服务器错误!");
		},
		success : function(resultData){
			var wholeHtml = '';
			var brandHtml = '';
			var seriesHtml = '';
			
			$.each(resultData,function(brandIndex,brandObj){
				brandHtml = '<h4>'+brandObj.brandName+'</h4>';
				brandHtml +='<ul class="list-group">';
				$.each(brandObj.series,function(seriesIndex,seriesObj){		
					seriesHtml = '<a onclick="selectModelPackage(this)" seriesCode="'+seriesObj.seriesCode+'" href="#" class="list-group-item">'+
								  '<span class="glyphicon glyphicon-chevron-right form-control-feedback"></span>'+
								  seriesObj.seriesName+
	                              '</a>';
					brandHtml +=seriesHtml;
				});
				
				brandHtml +='</ul>';
				
				wholeHtml +=brandHtml;
			});
			
	    	 $("#carModelExhibitionDiv").append(wholeHtml);
		     $("#vehicleBindingForm").removeClass("active in");
		     $("#carModelExhibitionDetailDiv").removeClass("active in");
		     $("#carModelExhibitionDiv").addClass("active in");
		     
		}
		
		
	});

}


//初始化加载车型配置页面
function initModelPackage(seriesCode){
	
	$("#carModelPackageExhibitionDiv").empty();
	$.ajax({
		method : 'get',
		url : rootPath+'/wechat/api/showroom/series?seriesCode='+seriesCode,
		error:function(XMLHttpRequest, ajaxOptions, thrownError){
			alert("连接服务器错误!");
		},
		success : function(resultData){
			if(resultData!=undefined && resultData!=''){
				$("#modelNameSpan").html(resultData.seriesName);
			}

			var wholeHtml = '';
			$.each(resultData.models,function(modelIndex,modelObj){
				
				var modelHtml =  '<ul class="list-group">'
							+'    <a href="#" class="list-group-item active">'
							+         modelObj.modelName
					        +'    </a>';
				
				$.each(modelObj.packages,function(packageIndex,packageObj){
					var packageHtml = '<a href="#" onclick="selectConcretePackage(this)" modelCode='+modelObj.modelCode+' packageCode='+packageObj.packageCode+' packageName='+packageObj.packageName+' class="list-group-item">'
							  	 +'  <span class="glyphicon glyphicon glyphicon-heart form-control-feedback"></span>'
							  	 +       packageObj.packageName
					             +'</a>';
					
					 
					modelHtml +=packageHtml;
					
				});
				
				modelHtml +='</ul>';
				
				wholeHtml +=modelHtml;
				
			});
			
			 $("#carModelPackageExhibitionDiv").append(wholeHtml);
		     $("#vehicleBindingForm").removeClass("active in");
		     $("#carModelExhibitionDiv").removeClass("active in");
			 $("#carModelExhibitionDetailDiv").addClass("active in");
			
		}
	});

}
 
 
 
 //选择车型配置页面
 function selectModelPackage(obj){
	 var seriesCode = $(obj).attr("seriesCode");
	 
	 $("#defaultSeriesCode").val(seriesCode);
	 initModelPackage(seriesCode);
 }
 
 
 //选择具体的车型配置
 function selectConcretePackage(obj){
	 var modelCode = $(obj).attr("modelCode");
	 var packageCode = $(obj).attr("packageCode");
//	 var packageName = $(obj).attr("packageName");
	 
	 var packageName = $(obj).text();
	 
	 $("#defaultPackageName").val(packageName);
	 $("#defaultModelCode").val(modelCode);
	 $("#defaultPackageCode").val(packageCode);
	 	 
	 
     $("#carModelExhibitionDiv").removeClass("active in");
	 $("#carModelExhibitionDetailDiv").removeClass("active in");
     $("#vehicleBindingForm").addClass("active in");
 }

 
 
